//
//  VipServiceNewViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/6/3.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "VipServiceNewViewController.h"
#import "FirstCollectionViewCell.h"
#import "SecondCollectionViewCell.h"
#import "NormalPrombleViewController.h"
#import "VIPWebViewController.h"
#import "LoginViewController.h"

static NSString *firstIdentifier =@"FirstCollectionViewCell";
static NSString *secondIdentifier =@"SecondCollectionViewCell";

@interface VipServiceNewViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UICollectionView *firstCollectionView;
@property (strong, nonatomic) IBOutlet UICollectionView *secondCollectionView;
@property (weak, nonatomic) IBOutlet UILabel *serviceNumber;
@property(nonatomic,strong)NSString * phoneStr;

@property (assign, nonatomic) NSArray *firstArray;
@property (assign, nonatomic) NSArray *secondArray;
@property (assign, nonatomic) NSArray *firstImageArray;
@property (assign, nonatomic) NSArray *secondImageArray;
@property (strong, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet UIImageView *levelBackImage;
@property (weak, nonatomic) IBOutlet UIImageView *levelImage;
@property (weak, nonatomic) IBOutlet UILabel *levelLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIImageView *loginImage;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *personType;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@end

@implementation VipServiceNewViewController

-(void)viewWillAppear:(BOOL)animated{
    if (![Common getLoginStatus]) {
        _levelBackImage.hidden = YES;
        _levelImage.hidden = YES;
        _levelLabel.hidden = YES;
        _phoneLabel.hidden = YES;
        _registerButton.hidden = NO;
        _loginButton.hidden = NO;
        _loginImage.hidden = NO;
        _personType.hidden = YES;

        
        for (UIView * view in self.backgroundView.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton * button = (UIButton *)view;
                if (button.tag>=1000&&button.tag<=1004) {
                    button.selected = NO;
                }
            }
        }
        UIView * orangeLine = [self.view viewWithTag:1005];
        orangeLine.hidden = YES;
        
        UIImageView * cirImage = [self.view viewWithTag:1006];
        cirImage.hidden = YES;

    }else{
        _levelBackImage.hidden = NO;
        _levelImage.hidden = NO;
        _levelLabel.hidden = NO;
        _phoneLabel.hidden = NO;
        _phoneLabel.text = [Common getLoginInfo][Key_User_userPhone];
        _registerButton.hidden = YES;
        _loginButton.hidden = YES;
        _loginImage.hidden = YES;
        [self querySysWebsiteConfig];
        [self getPersonInfo];
        
        NSDictionary * dic =  [Common getLoginInfo];
        _personType.hidden = NO;
        NSInteger userType = [dic[Key_User_suseType] integerValue];
        if (userType%16==0) {
            [_personType setTitle:@"普通用户" forState:UIControlStateNormal];
        }else if(userType%16%4%8==0){
            [_personType setTitle:@"商铺用户" forState:UIControlStateNormal];
        }else if(userType%16%4==0){
            [_personType setTitle:@"业主用户" forState:UIControlStateNormal];
        }else if(userType%16%8==0){
            [_personType setTitle:@"商铺用户" forState:UIControlStateNormal];
        }else if(userType%8==0){
            [_personType setTitle:@"商铺用户" forState:UIControlStateNormal];
        }else if(userType%8%4==0){
            [_personType setTitle:@"商铺用户" forState:UIControlStateNormal];
        }else if(userType%4==0){
            [_personType setTitle:@"业主用户" forState:UIControlStateNormal];
        }else if(userType%1==0){
            [_personType setTitle:@"管理员" forState:UIControlStateNormal];
        }else if(userType%1%8==0){
            [_personType setTitle:@"管理员" forState:UIControlStateNormal];
        }else if(userType%1%16==0){
            [_personType setTitle:@"管理员" forState:UIControlStateNormal];
        }else if(userType%1%4==0){
            [_personType setTitle:@"管理员" forState:UIControlStateNormal];
        }
        CGSize size = [_personType.titleLabel.text sizeWithFont:_personType.titleLabel.font maxSize:flexibleSize(CGSizeMake(100, 14), NO)];
        _personType.bounds = CGRectMake(0, 0, (size.width+5)*VerticalRatio(), (size.height+4)*VerticalRatio());
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayOut];
    [self querySysWebsiteConfig];
    [self getPersonInfo];
}

- (void)initViewLayOut{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scrollView];
    [self adaptiveViewLayout:self.backgroundView];
    _scrollView.frame = CGRectMake(0, 0,320*VerticalRatio(), 568*VerticalRatio()-49);
    _scrollView.contentSize = flexibleSize(CGSizeMake(0, 568), NO);
    
    _personType.layer.masksToBounds = YES;
    _personType.layer.cornerRadius = 2*VerticalRatio();
    
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0*VerticalRatio();
    flowLayout.minimumLineSpacing = 0*VerticalRatio();
    
    UICollectionViewFlowLayout *FlowLayout =[[UICollectionViewFlowLayout alloc]init];
    FlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    FlowLayout.minimumInteritemSpacing = 0*VerticalRatio();
    FlowLayout.minimumLineSpacing = 0*VerticalRatio();
    
    self.firstCollectionView.collectionViewLayout = flowLayout;
    self.secondCollectionView.collectionViewLayout = FlowLayout;
    self.firstCollectionView.backgroundColor = [UIColor clearColor];
    self.secondCollectionView.backgroundColor = [UIColor clearColor];
    
    [self.firstCollectionView registerNib:[UINib nibWithNibName:firstIdentifier bundle:nil] forCellWithReuseIdentifier:firstIdentifier];
    [self.secondCollectionView registerNib:[UINib nibWithNibName:secondIdentifier bundle:nil] forCellWithReuseIdentifier:secondIdentifier];
    self.firstCollectionView.delegate = self;
    self.firstCollectionView.dataSource = self;
//    self.secondCollectionView.delegate = self;
//    self.secondCollectionView.dataSource = self;

}

#pragma mark  == 拨号
- (IBAction)callHandel:(id)sender {
    [Common callPhone:_phoneStr view:self.view];
}

#pragma mark ===== 登录
- (IBAction)loginHandel:(id)sender {
    [self ShowLoginPage];
}
#pragma mark ===== 注册
- (IBAction)registerHandel:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    LoginViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Login"];
    page.ifRegister = YES;
    [self presentViewController:page animated:YES completion:^{}];
}

#pragma mark ===== 升级会员
- (IBAction)upLevelHandel:(id)sender {
    if (![Common getLoginStatus]) {
        [self ShowLoginPage];
        return;
    }
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"UpLevel"];
            [self.navigationController   pushViewController:page animated:YES];
}

#pragma mark == 获取用户信息
- (void)getPersonInfo{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryPersonalData withHttpType:@"POST" withTag:SX_SX_queryPersonalData];
}


#pragma mark == 获取客服电话
- (void)querySysWebsiteConfig{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_querySysWebsiteConfig withHttpType:@"POST" withTag:SX_querySysWebsiteConfigTag];
}

#pragma mark - <UICollectionViewDelegate&&UICollectionViewDataSource>
//定义 UICollectionView 的 cell 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _firstCollectionView) {
        return 9;
    } else {
        return 4;
    }
}

//定义UICollectionView Cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _firstCollectionView) {
        return CGSizeMake(106*VerticalRatio(), 90*VerticalRatio());
    } else {
        return CGSizeMake(79*VerticalRatio(), 72*VerticalRatio());
    }
}

//定义 UICollectionView 的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


//定义 UICollectionView cell 展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    if (collectionView == _firstCollectionView) {
        FirstCollectionViewCell *firstCell = [self.firstCollectionView dequeueReusableCellWithReuseIdentifier:firstIdentifier forIndexPath:indexPath];
        firstCell.backgroundColor = [UIColor clearColor];
//        NSArray * firstArray = @[@"平台指南",@"退换货",@"退款说明",@"常见问题",@"投诉建议",@"实名认证"];
         NSArray * firstArray = @[@"平台指南",@"退换服务",@"会员升级",@"会员特权",@"会员等级规则",@"会员积分计划",@"常见问题",@"投诉建议",@"实名认证"];

        
        NSArray * firstImageArray = @[@"VipServiceIcon1@2x.png",@"VipServiceIcon2@2x.png",@"VipServiceIcon3@2x.png",@"VipServiceIcon4@2x.png",@"VipServiceIcon5@2x.png",@"VipServiceIcon6@2x.png",@"VipServiceIcon9@2x.png",@"VipServiceIcon10@2x.png",@"VipServiceIcon11@2x.png"];
        firstCell.firstImage.image = [UIImage imageNamed:firstImageArray[indexPath.row]];
        firstCell.firstLabel.text = firstArray[indexPath.row];
        return firstCell;
    } else {
        SecondCollectionViewCell *secondCell = [self.secondCollectionView dequeueReusableCellWithReuseIdentifier:secondIdentifier forIndexPath:indexPath];
        secondCell.backgroundColor = [UIColor clearColor];
        NSArray * secondArray = @[@"VipService1@2x.png",@"VipService2@2x.png",@"VipService3@2x.png",@"VipService4@2x.png"];
        secondCell.secondImage.image = [UIImage imageNamed:secondArray[indexPath.row]];
         NSArray * secondTitles = @[@"会员特权",@"积分规则",@"会员等级规则",@"积分奖励计划"];
        secondCell.secondLabel.text = secondTitles[indexPath.row];
        return secondCell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _firstCollectionView) {
        if (indexPath.row==0) {//平台指南
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            NormalPrombleViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"NormalPromble"];
            page.typeStr = @"2";//使用帮助
            [self.navigationController  pushViewController:page animated:YES];
        }else if (indexPath.row==1) {//退换服务
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ReturnService"];
            [self.navigationController  pushViewController:page animated:YES];
        }else if (indexPath.row==2) {//会员升级
            
            if (![Common getLoginStatus]) {
                [self ShowLoginPage];
                return;
            }
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"UpLevel"];
            [self.navigationController   pushViewController:page animated:YES];
        }else if (indexPath.row==3) {//会员特权
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            VIPWebViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"VIPWeb"];
            page.url = SX_memberPrivilege;
            page.titleStr = @"会员特权";
            [self.navigationController  pushViewController:page animated:YES];
        }else if (indexPath.row==4) {//会员等级规则
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            VIPWebViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"VIPWeb"];
            page.url = SX_memberLevelRule;
            page.titleStr = @"会员等级规则";
            [self.navigationController  pushViewController:page animated:YES];
        }else if (indexPath.row == 5){//会员积分计划
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            VIPWebViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"VIPWeb"];
            page.url = SX_memberScorePrize;
            page.titleStr = @"会员积分计划";
            [self.navigationController  pushViewController:page animated:YES];

        }else if (indexPath.row == 6){//常见问题
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            NormalPrombleViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"NormalPromble"];
            page.typeStr = @"1";//常见问题
            [self.navigationController  pushViewController:page animated:YES];
            
        }else if (indexPath.row == 7){//投诉建议
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"CommitSuggest"];
            [self.navigationController  pushViewController:page animated:YES];
            
        }else if (indexPath.row == 8){//实名认证
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PersonalAttest"];
            [self.navigationController  pushViewController:page animated:YES];
            
        }
    }
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
     if (tag == SX_querySysWebsiteConfigTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            self.phoneStr = database[@"result"];
            self.serviceNumber.text = [NSString stringWithFormat:@"客服热线: %@",self.phoneStr];
            
        }
     }else if (tag==SX_SX_queryPersonalData) {
         
         if ([[database valueForKey:@"type"] integerValue]!=1 ) {
             
             [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
             
         }else  {
             NSArray * array = database[@"result"];
             if (array.count==0) {
                 return;
             }
             NSDictionary * dic = database[@"result"][0];
//             NSInteger  vipLevel = [[NSString stringWithFormat:@"%@",dic[@"memberVipImg"]] integerValue];
             NSInteger  vipLevel = 2;
             
             _levelImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"level%ld",vipLevel]];
             NSArray * levelArray = @[@"-普通会员-",@"-白银会员-",@"-黄金会员-",@"-铂金会员-",@"-钻石会员-"];
             _levelLabel.text = levelArray[vipLevel-1];
             
             for (UIView * view in self.backgroundView.subviews) {
                 if ([view isKindOfClass:[UIButton class]]) {
                     UIButton * button = (UIButton *)view;
                     if (button.tag>=1000&&button.tag<=1000+vipLevel-1) {
                         button.selected = YES;
                     }else if (button.tag>1000+vipLevel-1){
                         button.selected = NO;
                     }
                 }
            }
             
             CGFloat x = 65+60*(vipLevel-1);
             
             UIView * orangeLine = [self.view viewWithTag:1005];
             orangeLine.frame = flexibleFrame(CGRectMake(46, 192, x-46, 1.5), NO);
             orangeLine.hidden = NO;
             
             UIImageView * cirImage = [self.view viewWithTag:1006];
             cirImage.center = flexibleCenter(CGPointMake(x, 193));
             cirImage.hidden = NO;
             
         }
     }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
